package util;

import java.util.concurrent.*;

public class ThreadPoolExecutorUtil {

    private final static int corePoolSize = 10;                  // 核心线程数量
    private final static int maximumPoolSize =50;                // 最大线程梳理
    private final static int keepAliveTime = 100;                // 空余保量时间
    private final static int capacity = 3;                       // 队列数
    private final static TimeUnit unit = TimeUnit.MILLISECONDS;  // 时间单位

    /**
     * 定义一个线程池
     */
    public static ThreadPoolExecutor getThreadPoolExecutor() {
        // 队列
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(capacity);
        // 线程工厂
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        // 拒绝策略
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        // 线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                threadFactory,
                handler
        );
        return threadPoolExecutor;
    }
}
